<script setup lang="ts">
import { onMounted, onUnmounted, ref, shallowRef } from 'vue'
import { Scene, PerspectiveCamera, SphereGeometry, MeshStandardMaterial, Mesh, PlaneGeometry, AmbientLight, DirectionalLight, WebGLRenderer, OrthographicCamera } from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls'
import { GUI, GUIController } from 'dat.gui'

const gui = new GUI()
const contentRef = ref<HTMLCanvasElement>()
const renderer = shallowRef<WebGLRenderer>(new WebGLRenderer())
const orbitControls = shallowRef<OrbitControls>()
const scene = new Scene()
const camera = shallowRef<PerspectiveCamera>()
const guiControllers = ref<GUIController[]>([])

const material = new MeshStandardMaterial()

// 创建球体
const sphereGeometry = new SphereGeometry(1, 20, 20)
const mesh = new Mesh(sphereGeometry, material)
scene.add(mesh)

// 创建平面
const planeGeometry = new PlaneGeometry(10, 10)
const planeMesh = new Mesh(planeGeometry, material)
planeMesh.position.set(0, -1, 0)
planeMesh.rotation.x = -Math.PI / 2
scene.add(planeMesh)

// 创建灯光-环境光
const light = new AmbientLight('#ffffff', 0.5)
scene.add(light)
const decLight = new DirectionalLight('#ffffff', 0.5)
decLight.position.set(10, 10, 10)
scene.add(decLight)

const render = () => {
  if (camera.value) {
    orbitControls.value?.update()
    renderer.value?.render(scene, camera.value)
  }
  requestAnimationFrame(render)
}

onMounted(() => {
  if (contentRef.value) {
    camera.value = new PerspectiveCamera(75, contentRef.value.clientWidth / contentRef.value.clientHeight)
    camera.value.position.set(0, 10, 20)
    scene.add(camera.value)
    contentRef.value.appendChild(renderer.value.domElement)
    renderer.value.setSize(contentRef.value?.clientWidth, contentRef.value?.clientHeight)

    orbitControls.value = new OrbitControls(camera.value, renderer.value.domElement)
    orbitControls.value.enableDamping = true

    // 1. 渲染器阴影选项打开
    renderer.value.shadowMap.enabled = true
    // 2. 平行光投射阴影
    decLight.castShadow = true
    // 3. 物体投射阴影
    mesh.castShadow = true
    // 4. 平面接收阴影
    planeMesh.receiveShadow = true

    // 设置阴影模糊度
    decLight.shadow.radius = 20
    // 设置阴影贴图分辨率
    decLight.shadow.mapSize.set(2048, 2048)

    decLight.shadow.camera.near = 0.5
    decLight.shadow.camera.far = 500
    decLight.shadow.camera.top = 5
    decLight.shadow.camera.bottom = -5
    decLight.shadow.camera.left = -5
    decLight.shadow.camera.right = 5

    gui
      .add<any>(decLight.shadow.camera, 'near')
      .min(0)
      .max(20)
      .step(0.1)
      .onChange(() => {
        decLight.shadow.camera.updateProjectionMatrix()
      })

    render()
  }
})

onUnmounted(() => {
  gui.destroy()
})

window.addEventListener('resize', () => {
  if (contentRef.value && camera.value) {
    console.log('resize')
    camera.value.aspect = contentRef.value.clientWidth / contentRef.value.clientHeight
    camera.value.updateProjectionMatrix()

    renderer.value.setSize(contentRef.value?.clientWidth, contentRef.value?.clientHeight)
    renderer.value.setPixelRatio(window.devicePixelRatio)
  }
})
</script>

<template>
  <div class="base-light-shadow" ref="contentRef"></div>
</template>

<style lang="scss" scoped>
.base-light-shadow {
  width: 100%;
  height: 100%;
}
</style>
